import Vue from 'vue'
import App from './App.vue'
import router from './router'
// 引入axios
import axios from 'axios'
// 引入iviewui组件库
import './plugins/iviewui'
import 'view-design/dist/styles/iview.css'
// 引入全局样式
import './assets/css/global.css'
// 引入mavon-editor的markdown编辑器
import mavonEditor from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'

Vue.use(mavonEditor)
axios.defaults.baseURL = 'http://localhost:8080/edwardBlog/'
// 请求拦截器，在所有调用axios前进入拦截器

axios.interceptors.request.use(config => {
  // 将sessionStorage中的token存放到请求头的Authorization字段中（Authorization是服务器端指定的）
  config.headers.lhamster_identity_info = window.sessionStorage.getItem('lhamster_identity_info')
  // 指定为后台请求
  config.headers.type = 'back'
  return config
})

// 响应拦截器
axios.interceptors.response.use(response => {
  // 检验token是否过期，若过期则重新登录
  if (response.data.code === 4001 || response.data.code === 2001 || response.data.code === 4002) {
    // 清除本地token
    window.sessionStorage.removeItem('lhamster_identity_info')
    // 清除用户信息
    window.sessionStorage.removeItem('nickname')
    window.sessionStorage.removeItem('headpicture')
    window.sessionStorage.removeItem('admin')
    alert(response.data.msg);
    router.replace({
      path: '/login'
    })
  } else {
    return response
  }
})
// 将axios挂载到当前Vue的属性中
Vue.prototype.$http = axios
Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
